package ar.edu.uba.fi.jmit.monitorengine.mock;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ar.edu.uba.fi.jmit.integrationcommons.Event;
import ar.edu.uba.fi.jmit.integrationcommons.MessagePriority;
import ar.edu.uba.fi.jmit.sender.Endpoint;
import ar.edu.uba.fi.jmit.sender.Sender;
/**
 * Representa un Sender del proyecto Sender.
 * @author Tomas
 *
 */
public class SenderMock implements Sender {
	
	private Map<String, List<Event>> sendedEvents = new HashMap<String, List<Event>>();
	
	private Map<String, List<Event>> hpSendedEvents = new HashMap<String, List<Event>>();

	public void send(Event event) {
		this.send(event, MessagePriority.NORMAL);
	}
	
	private void addEvent(Map<String, List<Event>> map,
			String destination, Event event) {
		List<Event> eventList = map.get(destination);
		if(eventList == null) {
			eventList = new LinkedList<Event>();
			map.put(destination, eventList);
		}
		eventList.add(event);
	}

	public void send(Event event, MessagePriority messagePriority) {
		send(event, getDefaultDestination(), messagePriority);
	}

	public List<Event> getSendedEvents(String destination) {
		return sendedEvents.get(destination);
	}
	
	public List<Event> getHpSendedEvents(String destination) {
		return hpSendedEvents.get(destination);
	}
	
	public List<Event> getAllSendedEvents() {
		List<Event> list = new LinkedList<Event>();
		for(List<Event> eventList:sendedEvents.values()) {
			list.addAll(eventList);
		}
		for(List<Event> eventList:hpSendedEvents.values()) {
			list.addAll(eventList);
		}
		return list;
	}

	@Override
	public void addEndpoint(String destinationName, Endpoint endpoint) {
		throw new UnsupportedOperationException("Not implemented in mock");
	}

	@Override
	public String getDefaultDestination() {
		return "default";
	}

	@Override
	public void send(Event event, String destinationName) {
		addEvent(sendedEvents, destinationName, event);
		
	}

	@Override
	public void send(Event event, String destinationName,
			MessagePriority priority) {
		if(MessagePriority.HIGH_PRIORITY.equals(priority)) {
			addEvent(hpSendedEvents, destinationName, event);
		}else {
			addEvent(sendedEvents, destinationName, event);
		}
		
	}

	@Override
	public void send(Event event, Collection<String> destinationNames,
			MessagePriority priority) {
		for(String destination:destinationNames) {
			send(event, destination, priority);
		}
		
	}

	@Override
	public void send(Event event, Collection<String> destinationNames) {
		for(String destination:destinationNames) {
			send(event, destination, MessagePriority.NORMAL);
		}
	}

	@Override
	public void setDefaultDestination(String destinationName) {
		
	}
}
